package com.caiyouxi.common;

import com.caiyouxi.common.util.Log;
import com.caiyouxi.system.model.SettingsBean;
import com.caiyouxi.system.service.SysSetingService;
import com.caiyouxi.common.cache.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 初始化类，用于加载初始数据等
 * weichangyong
 *
 * @create 2017-08-28 16:44
 **/
@Component
public class SystemInit implements CommandLineRunner {

    @Autowired
    SysSetingService sysSetingService;
    @Autowired
    RedisService redisService;

    @Override
    public void run(String... strings) throws Exception {

        Log.d(">>>>>>>>>>>>>>>init data start...<<<<<<<<<<<<<");
        init_system_setting();
        Log.d(">>>>>>>>>>>>>>>init data end...<<<<<<<<<<<<<");
    }

    /**
     * 初始化系统全局设计到缓存
     */
    public void init_system_setting()
    {

        Log.d("移除redis....");
        //redisService.flushDB();

        Log.d("初始化系统全局设置到缓存");
        List<SettingsBean>  settingsBeanList = sysSetingService.selectAll();
        for (int i = 0; i <settingsBeanList.size() ; i++) {
            redisService.set(settingsBeanList.get(i).getKey(),settingsBeanList.get(i).getValue());
        }

    }


}
